package com.zp.self.module.返回xml结构数据;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.zp.self.module.返回xml结构数据.工作中AEMP的使用.Equipment;
import com.zp.self.module.返回xml结构数据.工作中AEMP的使用.EquipmentHeader;
import com.zp.self.module.返回xml结构数据.工作中AEMP的使用.LocationInfo;
import com.zp.self.module.返回xml结构数据.重写序列化方式.XmlJsonSerializableCommon;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author ：by peng.zeng
 * @date ：Created 2021/5/21 13:44
 * @description：尝试与测试返回xml格式结构，并在标签中添加属性
 *
 * 方式1：定义返回类型此时 Content-Type【好像是：Accept ??】 默认为 application/xhtml+xml;charset=UTF-8 格式，
 * 可以手动改变下类型：application/xml;charset=UTF-8   。
 *
 * 方式2：@RequestMapping(value = "/test" produces = MediaType.APPLICATION_XML_VALUE)表明是用 application/xml 格式输出数据。
 */
@RestController
@RequestMapping("/xml")
@Validated
public class XmlController {

    @GetMapping(value = "/getForce",produces = MediaType.APPLICATION_XML_VALUE)
    public Object getForce(){
        return new Person().setName("张三").setAge(1).setBirthday(new Date());
    }

    @GetMapping(value = "/get")
    public Object get(){
        Person person = new Person();
        person.setIdsdad("zhuan2quan");
        person.setName("程序新视界");
        person.setInterest(new String[]{"Java", "Spring Boot", "JVM"});
        return  person;
    }

    /**
     * Description: AEMP相关的API  编写
     * @Author: peng.zeng
     * @DateTime: 2021/5/21 17:16
     * @return java.lang.Object
     */
    @GetMapping(value = "/getEquipment",headers = {"Accept=application/x.iso15143-3.v20161201+xml","Accept=application/x.iso15143-3.v20161201+json"})
    public Object getEquipment(@Valid @RequestParam(required = false,defaultValue = "10")  @Max(value = 100, message = "name最大长度为100") int limit){

        LocationInfo local =  LocationInfo.builder().datetime(new Date().toString()).latitude("123.22").longitude("73.11").build();
        LocationInfo local2 =  LocationInfo.builder().datetime(new Date().toString()).latitude("53.22").longitude("13.11").build();
        LocationInfo local3 =  LocationInfo.builder().datetime(new Date().toString()).latitude("33.22").longitude("33.11").build();
        LocationInfo[]  l= new LocationInfo[] {local,local2,local3};

        List<LocationInfo> locationInfos = Arrays.asList(l);
        EquipmentHeader header =EquipmentHeader.builder().oemname("JOHN DEERE").model("325G")
               .pin("222").serialNumber("111").unitInstallDateTime(new Date().toString()).build();
        return    Equipment.builder().listLocal1(locationInfos).header(header).build();
    }

    @RequestMapping(value = "/Fleet/{pageNumber}",method = RequestMethod.GET)
    // TODO 后期加上Accept header 必须的格式
    public Object getFleet( @Valid  @Max(value = 100, message = "The maximum length of PageNumber is 100") @Min(value = 0, message = "The minimum value of PageNumber is 0")
                           @PathVariable int pageNumber,
                           @Valid  @Max(value = 100, message = "The maximum length of limit is 100") @Min(value = 0, message = "The minimum limit is 0")
                           @RequestParam(required = false,defaultValue = "10") int limit) {
        System.out.println(pageNumber+"--"+limit);
        return "OK";
    }

    /**
     * Description: 自己去重写了序列化，有点多余了啊
     */
    @GetMapping(value = "/getAndAttributes--old")
    public Object getAndAttributesOld(){
        XmlJsonSerializableCommon resout = new XmlJsonSerializableCommon();
        resout.attributesMap.put("version","2");
        resout.attributesMap.put("snapshotTime",new Date().toString());

        Person person = new Person();
        person.setIdsdad("zhuan2quan");
        person.setName("程序新视界");
        person.setInterest(new String[]{"Java", "Spring Boot", "JVM"});
        resout.ObjectFieldMap.put("Person",person);
        XmlMapper xmlMapper = new XmlMapper();
        try {
            return xmlMapper.writeValueAsString(resout);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return  null;
    }
}
